home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 6 / MacMania 6.toast / / Multimedia & Desktop / VideoToolbox / VideoToolboxSources / IsCmdPeriod.c < prev    next >
Text File  |  1997-04-10  |  5KB  |  121 lines

  1. /*
  2. IsCmdPeriod.c
  3. You pass it an event record and it tells you whether or not the user has hit
  4. command-period (i.e. "cancel"). This method should work on all Macs, including
  5. international Mac OS's that remap the keyboard.
  6.  
  7. You should also look at CommandPeriod.c.
  8.  
  9. WARNING:
  10. Hasn't been tested much. The caching scheme (saving "period") might fail if you
  11. change script systems between calls to IsCmdPeriod.
  12.  
  13. Copied from comp.sys.mac.programming:
  14.  
  15. From: k044477@hobbes.kzoo.edu (Jamie R. McCarthy)
  16. Date: 13 May 93 19:41:38 GMT
  17. Organization: Kalamazoo College
  18.  
  19. >PLEASE everybody read the "International Canceling" tech note (in
  20. >"Text"), or in the official jargon: M.TE.InternationalCancel (yech).
  21. >
  22. >It explains that Command key combinations with non-letter keys may
  23. >map to Command-Shift key codes on international keyboards. And we
  24. >all know that Command-Shift gets intercepted by the operating system.
  25. >This is a pain, because it means you won't be able to cancel operations,
  26. >or have access to similar keyboard shortcuts.
  27.  
  28. Here's the function I use.  I think I took it pretty much straight from
  29. the TN, but I might have made some minor improvement, I don't recall
  30. what.  Anyway, maybe this'll save someone a few minutes' worth of
  31. typing:
  32. - -- 
  33.  Jamie McCarthy     Internet: k044477@kzoo.edu    AppleLink: j.mccarthy
  34.  
  35. HISTORY:
  36. 13 May 93 Jamie McCarthy posted it on comp.sys.mac.programming
  37. 6/23/93    dgp corrected bug of using "=" instead of "==" in an if statement.
  38.     Check for valid handle before dereferencing. Tightened up code a bit.
  39.     Not tested. 
  40. 6/30/93    dgp The elaborate method required for international systems seems
  41. excessive for routine use, so I added a shortcut. We go through the whole 
  42. rigamarole once, and note what the Cmd-period key presses were translated to.
  43. On subsequent occasions we just (quickly) check for that. 
  44. 5/22/95 dgp Apple changed the prototype from KeyTrans(...,long *state) 
  45.         to KeyTranslate(...,unsigned long *state). To make the code compatible
  46.         with both versions, I cast the argument as (void *).
  47. 11/10/95 dgp Daniel Sears, sears@netcom.com, writes "IsCmdPeriod.c should take advantage of
  48. IsCmdChar(event, '.'), which is international and was divulged in a Develop Q/A."
  49. I looked for this on the current Apple Developer CD (November '95) and the two preceding ones,
  50. and can't find it.I did find Tech Note TE 23 International Canceling, dated 1990, which 
  51. clearly predates the existence of IsCmdChar and goes into a long
  52. explanation and supplies sample code, which I suppose is the basis of what's below, though
  53. I haven't checked. I did find the prototype for IsCmdChar in Script.h though, and went ahead and made
  54. the change, as suggested by Dan.
  55. 3/19/97    dgp David Brainard noticed that we were treating plain period as equivalent to command-period.
  56.             It turns out that I hadn't read the documentation for IsCmdChar() carefully enough. It
  57.             doesn't check for the Command key, so I've added an explicit test for the command key.
  58.             I also allow for an "autoKey" as well as a keyDown event.
  59. 3/19/97    dgp Updated test for IsCmdChar() trap based on advice from Apple to just check for System 7.
  60. 4/10/97    dgp    Eliminate stuff that was conditional on !UNIVERSAL_HEADERS.
  61. */
  62. #include "VideoToolbox.h" 
  63. //#include <Types.h>
  64. //#include <OSUtils.h>
  65. // IsCmdChar() is documented in develop 22 Q&A.
  66. // A copy of that documentation appears in the VideoToolbox file CommandPeriod.c
  67. #ifndef __SCRIPT__
  68.     #include <Script.h>        // IsCmdChar
  69. #endif
  70.  
  71. Boolean IsCmdPeriod(register EventRecord *event)
  72. {
  73.     static char period=0;    // what cmd-period gets mapped to.
  74.     static Boolean firstTime=1,trapAvailable;
  75.     long version;
  76.  
  77.     if(firstTime){
  78.         // Is IsCmdChar() trap available?
  79.         Gestalt(gestaltSystemVersion,&version);
  80.         trapAvailable=(version>=0x700);
  81.         firstTime=0;
  82.     }
  83.     if(trapAvailable)
  84.         return IsCmdChar(event,'.') && (event->modifiers & cmdKey) && (event->what == keyDown || event->what == autoKey);    // prototype in Script.h. Documented in develop 22 Q&A.
  85.     else{
  86.         if(period!=0) return (event->what == keyDown || event->what == autoKey)
  87.             && (event->modifiers & cmdKey)
  88.             && (event->message & charCodeMask) == period;
  89.         else{
  90.             Boolean isCmdPeriod=FALSE;
  91.             short keyCode;
  92.             long keyInfo,keyCID;
  93.             unsigned long state;
  94.             Handle hKCHR;
  95.             Ptr pKCHR;
  96.             
  97.             if ((event->modifiers & cmdKey)
  98.                 && (event->what == keyDown || event->what == autoKey)){
  99.                 pKCHR=(Ptr)GetEnvirons(smKCHRCache);    // returns NULL under System 6
  100.                 if(pKCHR == NULL){
  101.                     keyCID=GetScript(GetEnvirons(smKeyScript),smScriptKeys);
  102.                     hKCHR=GetResource('KCHR',keyCID);
  103.                     if(hKCHR!=NULL)pKCHR=*hKCHR;
  104.                 }else hKCHR=NULL;
  105.                 if(pKCHR != NULL){
  106.                     // re-translate the virtual key to a char, without the command modifier
  107.                     keyCode=(event->message & keyCodeMask) >> 8;    // the virtual key
  108.                     keyCode |= event->modifiers & 0xFF00 & ~cmdKey;
  109.                     state=0;
  110.                     keyInfo=KeyTrans(pKCHR,keyCode,(void *)&state);    // compatible with all prototypes
  111.                     if((keyInfo&charCodeMask) == '.' 
  112.                         || ((keyInfo>>16)&charCodeMask) == '.')isCmdPeriod=TRUE;
  113.                     if (hKCHR != NULL) ReleaseResource(hKCHR);
  114.                 }else if((event->message&charCodeMask) == '.')isCmdPeriod=TRUE;
  115.                 if(isCmdPeriod)period=event->message&charCodeMask;
  116.             }
  117.             return isCmdPeriod;
  118.         }
  119.     }
  120. }
  121.